/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webidewar.system;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author huy
 */
public class inout extends Object {

    int count = 0;
    String input = "";
    String output = "";

    public synchronized String getInput() {
        while (!input.endsWith("\n")) {
            try {
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(inout.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        notifyAll();
        return input;
    }

    public synchronized void setInput(String _input) {
        while (this.input.endsWith("\n")) {
            try {
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(inout.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        this.input = _input;
        notifyAll();
    }

    public String getOutput() {
        return output;
    }

    public void setOutput(String output) {
        this.output = output;
    }

    public int nextInputByte() {
        String inp = getInput();

        if (count >= inp.length()) {
            return -1;
        }

        int i = (int) inp.getBytes()[count];
        count++;

        return i;
    }

    public void putOutputByte(int i) {
        output += (char) i;
    }
}
